<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 
        方法①：裁剪加拼接   
        方法②：先反转前n个，然后反转后面的，最后整个字符串反转
     -->
    <script>
      var reverseLeftWords = function (s, n) {
        let newArr = s.split("");
        reverse(newArr, 0, n - 1);
        reverse(newArr, n, newArr.length - 1);
        reverse(newArr, 0, newArr.length - 1);
        return newArr.join("");
        function reverse(arr, left, right) {
          while (left <= right) {
            [arr[left], arr[right]] = [arr[right], arr[left]];
            left++;
            right--;
          }
        }
      };
      reverseLeftWords("abcdefg", 2);
    </script>
  </body>
</html>
